// LeftView.cpp : implementation of the CLeftView class
//

#include "stdafx.h"
#include "ScenesTool.h"

#include "SceneDoc.h"
#include "LeftView.h"
#include "ChildFrm.h"
#include "SceneView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLeftView

IMPLEMENT_DYNCREATE(CLeftView, CTreeView)

BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
	//{{AFX_MSG_MAP(CLeftView)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeftView construction/destruction

CLeftView::CLeftView()
{
}

CLeftView::~CLeftView()
{
}

BOOL CLeftView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLeftView drawing

void CLeftView::OnDraw(CDC* pDC)
{
	CSceneDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}


void CLeftView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();

	CTreeCtrl& tree = GetTreeCtrl();

	tree.ModifyStyle(0, TVS_HASBUTTONS|TVS_SHOWSELALWAYS|TVS_EDITLABELS);

	HTREEITEM hRoot = tree.GetRootItem();

	CSceneDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	list<GROUP*>::iterator it;
	it = pDoc->m_Groups.begin();
	while(it != pDoc->m_Groups.end())
	{
		GROUP* pGroup = *it++;

		HTREEITEM hGroup = tree.InsertItem(pGroup->szName, hRoot);
		tree.SetItemData(hGroup, (DWORD)pGroup);

		list<TOBJECT*>::iterator lit;
		lit = pGroup->ObjList.begin();
		while(lit != pGroup->ObjList.end())
		{
			TOBJECT* pObject = *lit++;

			HTREEITEM hItem = tree.InsertItem(pObject->szName, hGroup);
			tree.SetItemData(hItem, (DWORD)pObject);
		}
	}

}

/////////////////////////////////////////////////////////////////////////////
// CLeftView diagnostics

#ifdef _DEBUG
void CLeftView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CLeftView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CSceneDoc* CLeftView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSceneDoc)));
	return (CSceneDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLeftView message handlers

void CLeftView::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM hSelect = pNMTreeView->itemNew.hItem;

	OnSelect(hSelect);
	
	*pResult = 0;
}


void CLeftView::OnSelect(HTREEITEM hSelect)
{
	CSceneDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	if(hSelect)
	{
		CChildFrame* pFrameWnd = (CChildFrame*)GetParentFrame();
		if(pFrameWnd)
		{
			CSceneView* pSceneView = pFrameWnd->GetSceneView();
			if(pSceneView)
			{
				CTreeCtrl& tree = GetTreeCtrl();
				TOBJECT* pObject = (TOBJECT*)tree.GetItemData(hSelect);
				if(pSceneView->m_pSelect != pObject)
				{
					pSceneView->m_pSelect = pObject;
					pSceneView->Invalidate();
				}
				
			}
		}
	}
}
